{extend name="base" /}

{block name="content"}
<button class="layui-btn layui-btn-normal" id="notice">结算通知</button>
{if condition="$disabled_btn eq 1"}
<button class="layui-btn layui-btn-normal" id="apply" style="margin-left: 0px;">申请结算</button>
{/if}

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="statusTpl">
    {{#  if(d.status === '已过期'){ }}
    <span class="layui-bg-cyan status4" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '确认中') { }}
    <span class="layui-bg-orange status1" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已确认') { }}
    <span class="layui-bg-blue status2" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已转账') { }}
    <span class="layui-bg-green status3" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } }}
</script>

<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="view">查看</button>
    {{# if(d.status == '确认中'){ }}
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="sure">确认账单</button>
    {{#  }else{ }}
    <button style="margin-left:10px;" class="layui-btn-sm layui-btn-disabled" disabled lay-event="sure">确认账单</button>
    {{#  } }}
</script>
{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use(['table', 'form', 'layer'], function(){
        var table = layui.table
            ,$ = layui.$
            ,layer = layui.layer
            ,load = layer.load();

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-80'
            ,url: '{:url("AuditList/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,resize: true
            ,done: function (res, curr, count) {
                layer.close(load);
            }
            ,cols: [[
                {field:'daytime', title: '对账日期', width: 200}
                ,{field:'cps_id', title: '渠道ID', width: 80}
                ,{field:'cps_name', title: '渠道名称', width: 130}
                ,{field:'fact_money', title: '充值金额', width: 100, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'pay_rate', title: '分成比例', width: 90}
                ,{field:'channel_rate', title: '通道费率', width: 90}
                ,{field:'tax_rate', title: '税率', width: 90}
                ,{field:'end_money', title: '分成金额', width: 100, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'create_time', title: '系统发送时间', width: 170}
                ,{field:'contact_man', title: '收款人', width: 100}
                ,{field:'open_account', title: '银行账号', width: 200}
                ,{field:'open_name', title: '所属银行', width: 200}
                ,{field:'open_bank', title: '开户银行', width: 200}
                ,{field:'description', title: '备注', width: 400}
                ,{field:'status', title: '状态', width: 100, templet: '#statusTpl', fixed: 'right'}
                ,{field:'operate', title: '操作', width: 300, align:'center', toolbar:'#tableBar', fixed: 'right'}
            ]]
        });

        $('#apply').click(function(){
            layer.confirm('确认要申请结算吗？', {offset: '250px'}, function(index){
                var load = layer.load();
                $.post('{:url("AuditList/apply")}',{}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        layer.msg(json.msg, {icon: 1, time:3000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:3000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            });
        })
        $('#notice').click(function(){
            layer.open({
                type: 1
                ,title: '结算通知'
                ,area: ['1200px', '700px']
                ,offset: '60px'
                ,resize: true
                ,content: '<div style="padding: 20px;"><div><strong style="font-size:16px;">{$notice1}</strong></div><div style="margin: 5px 0 10px 0;">{$notice2}</div><div>'
            });
        })

        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data
            if(obj.event === 'sure'){
                layer.confirm('确认账单信息吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("AuditList/sure")}',{id: data.id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            $(obj.tr).find("td[data-field='status']").html('<div class="layui-table-cell laytable-cell-1-status"><span class="layui-bg-blue status2" style="padding: 4px 6px;">已确认</span></div>');

                            $(obj.tr).find("td[data-field='operate']").find("button[lay-event='sure']").addClass('layui-btn-disabled').removeClass('layui-btn-normal layui-btn').attr('disabled', true).css('margin-left', '10px');

                            layer.msg(json.msg, {icon: 1, time:3000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:3000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }else if(obj.event === 'view'){
                layer.open({
                    type: 2
                    ,title: '查看'
                    ,offset: '60px'
                    ,resize: true
                    ,area: ['1200px', '700px']
                    ,content: '{:url("AuditList/view")}?id='+data.id+'&fact_money='+data.fact_money+'&end_money='+data.end_money
                });
            }
        });
    });
</script>
{/block}